package com.jg.xmlSchema2JsonSchema.xsd.util;

import com.jg.xmlSchema2JsonSchema.XmlUtil;
import com.jg.xmlSchema2JsonSchema.xsd.*;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.List;


public class XsdUtil {


    public static XsAnnotation getAnnotation(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ANNOTATION);
        if (elementList.size() > 0) {
            return new XsAnnotation(elementList.get(0));
        }
        return null;
    }

    public static XsSequence getSequence(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_SEQUENCE);
        if (elementList.size() > 0) {
            return new XsSequence(elementList.get(0));
        }
        return null;
    }


    public static XsComplexType getComplexType(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_COMPLEX_TYPE);
        if (elementList.size() > 0) {
            return new XsComplexType(elementList.get(0));
        }
        return null;

    }

    public static List<XsUnique> getUniques(Element element) {
        List<XsUnique> uniques = new ArrayList<XsUnique>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_UNIQUE);
        for (Element value : elementList) {
            uniques.add(new XsUnique(value));
        }
        return uniques;
    }

    public static List<XsAttribute> getAttributes(Element element) {
        List<XsAttribute> attributes = new ArrayList<XsAttribute>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ATTRIBUTE);
        for (Element value : elementList) {
            attributes.add(new XsAttribute(value));
        }
        return attributes;
    }

    public static List<XsElement> getElements(Element element) {
        List<XsElement> elements = new ArrayList<XsElement>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ELEMENT);
        for (Element value : elementList) {
            elements.add(new XsElement(value));
        }
        return elements;
    }

    public static List<Object> getElementsAndChoices(Element element) {
        List<Object> elements = new ArrayList<Object>();
        List<Element> elementList = XmlUtil.getChildElements(element, new String[]{XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ELEMENT, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_CHOICE});
        for (Element value : elementList) {
            if (value.getNodeName().equals(XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ELEMENT)) {
                elements.add(new XsElement(value));
            } else if (value.getNodeName().equals(XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_CHOICE)) {
                elements.add(new XsChoice(value));
            }
        }
        return elements;
    }

    public static XsSelector getSelector(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_SELECTOR);
        if (elementList.size() > 0) {
            return new XsSelector(elementList.get(0));
        }
        return null;
    }

    public static XsField getField(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_FIELD);
        if (elementList.size() > 0) {
            return new XsField(elementList.get(0));
        }
        return null;
    }


    public static List<XsEnumeration> getEnumerations(Element element) {
        List<XsEnumeration> elements = new ArrayList<XsEnumeration>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ENUMERATION);
        for (Element value : elementList) {
            elements.add(new XsEnumeration(value));
        }
        return elements;
    }

    public static List<XsPattern> getPatterns(Element element) {
        List<XsPattern> elements = new ArrayList<XsPattern>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_PATTERN);
        for (Element value : elementList) {
            elements.add(new XsPattern(value));
        }
        return elements;
    }


    public static List<XsChoice> getChoices(Element element) {
        List<XsChoice> tempChoice = new ArrayList<XsChoice>();
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_CHOICE);
        for (Element value : elementList) {
            tempChoice.add(new XsChoice(value));
        }
        return tempChoice;
    }


    public static XsRestriction getRestriction(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_RESTRICTION);
        if (elementList.size() > 0) {
            return new XsRestriction(elementList.get(0));
        }
        return null;
    }


    public static XsSimpleType getSimpleType(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_SIMPLETYPE);
        if (elementList.size() > 0) {
            return new XsSimpleType(elementList.get(0));
        }
        return null;
    }

    public static XsExtension getExtension(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_EXTENSION);
        if (elementList.size() > 0) {
            return new XsExtension(elementList.get(0));
        }
        return null;
    }

    public static XsComplexContent getComplexContent(Element element) {

        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_COMPLEXCONTENT);
        if (elementList.size() > 0) {
            return new XsComplexContent(elementList.get(0));
        }
        return null;
    }

    public static XsAll getAll(Element element) {
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_ALL);
        if (elementList.size() > 0) {
            return new XsAll(elementList.get(0));
        }
        return null;
    }

    public static List<XsEnumeration> getEnumerations(XsAttribute attribute) {
        XsSimpleType simpleType = attribute.getSimpleType();
        List<XsEnumeration> resultList = simpleType.getRestriction().getEnumerations();
        return resultList;
    }

    public static List<String> enumerationsToStringArray(List<XsEnumeration> enumerations) {
        List<String> items = new ArrayList<String>();
        for (XsEnumeration enumeration : enumerations) {
            items.add(enumeration.getValue());
        }
        return items;
    }

    public static List<EnumItem> enumerationsToEnumItemList(List<XsEnumeration> enumerations) {
        List<EnumItem> items = new ArrayList<>();
        for (XsEnumeration enumeration : enumerations) {
            items.add(EnumItem.builder().value(enumeration.getValue()).desc(enumeration.getAnnotation()).build());
        }
        return items;
    }


    public static List<String> patternsToStringArray(List<XsPattern> patterns) {
        List<String> items = new ArrayList<String>();
        for (XsPattern pattern : patterns) {
            items.add(pattern.getValue());
        }
        return items;
    }

    public static List<EnumItem> patternsToEnumItemListArray(List<XsPattern> patterns) {
        List<EnumItem> items = new ArrayList<>();
        for (XsPattern pattern : patterns) {
            items.add(EnumItem.builder().value(pattern.getValue()).desc(pattern.getAnnotation()).build());
        }
        return items;
    }

    public static XsLength getXsLength(Element element) {
        // TODO Auto-generated method stub
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_LENGTH);
        if (elementList.size() > 0) {
            return new XsLength(elementList.get(0));
        }
        return null;
    }

    public static XsMinLength getXsMinLength(Element element) {
        // TODO Auto-generated method stub
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_MINLENGTH);
        if (elementList.size() > 0) {
            return new XsMinLength(elementList.get(0));
        }
        return null;
    }

    public static XsMaxLength getXsMaxLength(Element element) {
        // TODO Auto-generated method stub
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_MAXLENGTH);
        if (elementList.size() > 0) {
            return new XsMaxLength(elementList.get(0));
        }
        return null;
    }

    public static XsWhiteSpace getXsWhiteSpace(Element element) {
        // TODO Auto-generated method stub
        List<Element> elementList = XmlUtil.getChildElements(element, XMLConstants.XSD_DEFAULT_NAMESPACE + ":" + XMLConstants.XSD_WHITESPACE);
        if (elementList.size() > 0) {
            return new XsWhiteSpace(elementList.get(0));
        }
        return null;
    }

}
